<template>
	<view class="content">
		<baidu-map class="mapbox" :center="center" :zoom="zoom" :dragging="dragging" @touchmove="touchmove"
			@touchstart="touchstart" @touchend="touchend" @moving="syncCenterAndZoom" @moveend="syncCenterAndZoom"
			@zoomend="syncCenterAndZoom" @ready="ready">
			<bm-control>
				<u-navbarmap :height="60" :autoBack="false">
					<template #center>
						<view class="searchbtn" @click="gosearchStationCar">
							<view class="placeholder">{{$z.$t("查找电站")}}</view>
							<view class="iconfont icon-fangdajing"></view>
						</view>
					</template>
					<template #left>
						<view class="u-flex" style="font-weight: bold" @click="getwz">
							<text class="u-line-1" style="width: 100rpx; margin-right: 8rpx">
								{{ city }}
							</text>
							<u-icon name="arrow-down"></u-icon>
						</view>
					</template>
					<template #right>
						<view class="iconfont icon-shaixuan" @click="sxfn"></view>
					</template>
				</u-navbarmap>
				<view class="bottonbox">
					<view class="wzitem iconfont icon-dingwei1" @click="wzitemtap"></view>
					<caritem :item="showitem" v-if="showitem.id && showitem.id != -1"></caritem>
				</view>
			</bm-control>

			<myoverlay :position="center" v-for="(item, key) in showlist" :key="key" :item="item" :showitem="showitem"
				@itemtap="itemtap"></myoverlay>
		</baidu-map>
		<ztabbar></ztabbar>
		<pt ref="pt" @save="save"></pt>
	</view>
</template>

<script>
	import pt from "./components/pt.vue";
	import logo from "@/static/logo.png";
	import myoverlay from "./components/MyOverlay.vue";
	import cityjs from "@/pages/city/cityjs.js";
	// console.log(logo);
	export default {
		components: {
			myoverlay,
			pt,
		},
		data() {
			return {
				center: {
					lat: 30.30593658246207,
					lng: 120.17779051925028,
				},
				zoom: 15,
				scrollTop: 0,
				wzlist: [],
				list: [],
				showitem: {},
				ptlist: [],
				dragging: false,
			};
		},
		mounted() {
			// this.init();
		},
		computed: {
			city() {
				if (this.$store.state.config.city.label) {
					return this.$store.state.config.city.label;
				}
				if (this.wzlist.length == 0) {
					return "";
				}
				let cityjsxs = cityjs.flat();
				let r = cityjsxs.find((e) => {
					return e.value == this.wzlist[0].AreaCode;
				});

				return r ? r.label : "";
			},
			showlist() {
				let ptlist = this.$u.deepClone(this.ptlist);
				let showlist = this.list.filter((e) => {
					if (ptlist.length == 0) {
						return true;
					}
					let kmclist = ptlist.filter((e) => {
						return e == this.$z.$t("快充") || e == this.$z.$t("慢充");
					});
					if (kmclist.length == 1) {
						let kmclista = [{
								name: this.$z.$t("快充"),
								value: "carzlsum",
							},
							{
								name: this.$z.$t("慢充"),
								value: "carjlsum",
							},
						];
						let kmclistb = kmclista.filter((e) => {
							return kmclist.indexOf(e.name) > -1;
						});
						if (e[kmclistb[0].value] == 0) {
							return false;
						}
					}
					let par1 = e.par1.split(",").filter((e) => {
						return e != "";
					});
					let par2 = ptlist.filter((e) => {
						return e != this.$z.$t("快充") && e != this.$z.$t("慢充");
					});
					if (par2.length == 0) {
						return true;
					}
					return par2.every((e) => {
						return par1.includes(e);
					});
				});
				return showlist;
			},
			reqsearchdata() {
				let r = {};
				if (this.$store.state.config.city.value) {
					r.AreaCode = this.$store.state.config.city.value;
				} else {
					let wz = this.$store.state.config.wz;
					r.pt = `POINT(${wz.lng},${wz.lat})`;
				}

				return r;
			},
		},
		methods: {
			save(list) {
				this.ptlist = list;
			},
			sxfn() {
				this.$refs.pt.show();
			},
			touchstart() {
				// console.log("touchstart");
			},
			touchmove() {
				this.dragging = true;
				// console.log("touchmove");
			},
			touchend() {
				this.dragging = false;
				// console.log("touchend");
			},
			gosearchStationCar() {
				uni.navigateTo({
					url: "/pages/searchStationCar/searchStationCar",
				});
			},
			getwz() {
				uni.navigateTo({
					url: "/pages/city/city",
				});
			},
			async ready() {
				if (Object.keys(this.$store.state.config.wz).length == 0) {
					await this.$store.dispatch("config/getLocation");
				}

				this.center = {
					...this.center,
					...this.$store.state.config.wz,
				};
				// console.log(this.center, "    this.center ");
				let list = await this.$z.request("/StationCar", this.reqsearchdata);
				setTimeout(() => {
					this.list = list;
				}, 300)
				this.wzlist = list;
				// this.dragging = false;
			},
			async wzinit() {
				let city = this.$store.state.config.city;
				if (city.wz) {
					this.center = {
						lat: city.wz[1],
						lng: city.wz[0],
					};
					this.ready()
				}

				console.log(this.$store.state.config.city);
			},
			itemtap(item) {
				this.showitem = {
					...item,
				};
			},
			async wzitemtap() {
				if (Object.keys(this.$store.state.config.wz).length == 0) {
					await this.$store.dispatch("config/getLocation");
				}

				this.center = {
					...this.$store.state.config.wz,
				};
				this.showitem.id = -1;
				this.zoom = 15
			},
			syncCenterAndZoom(e) {
				try {
					const {
						lng,
						lat
					} = e.target.getCenter();
					this.center.lng = lng;
					this.center.lat = lat;
					this.zoom = e.target.getZoom();
				} catch {

				}

			},
		},
	};
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		height: 100vh;

		.mapbox {
			// flex: 1;
			height: 100%;
			position: relative;

			.mapcoverview {
				position: absolute;
				bottom: 0;
				width: 100%;

				.dw {
					width: 80rpx;
					height: 80rpx;
					background: #fff;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 40rpx;
					border-radius: 20rpx;
					box-shadow: 0px 0px 12rpx rgba(255, 255, 255, 0.2);
					margin-left: 32rpx;
				}
			}
		}
	}

	.bottonbox {
		position: fixed;
		bottom: 0;
		padding: 0 30rpx;
		padding-bottom: 160rpx;

		left: 0;
		right: 0;

		.wzitem {
			width: 80rpx;
			height: 80rpx;
			border-radius: 10rpx;
			font-size: 50rpx;
			background: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
			margin-bottom: 20rpx;
		}
	}

	.searchbtn {
		width: 400rpx;
		background-color: #fff;
		height: 80rpx;
		border-radius: 8rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.iconfont {
			width: 80rpx;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: $u-primary;
			font-size: 40rpx;
		}

		.placeholder {
			font-size: 28rpx;
			color: $uni-text-color-placeholder;
			padding-left: 20rpx;
		}
	}

	.icon-shaixuan {
		background: #fff;
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 10rpx;
		color: $uni-color-primary;
	}
</style>